package com.seadragon.app.ksr.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seadragon.app.ksr.model.Business;
import com.seadragon.app.ksr.model.Event;
import com.seadragon.app.ksr.model.EventSchedule;
import com.seadragon.app.ksr.model.KsrConstants;
import com.seadragon.app.ksr.service.EventScheduleService;
import com.seadragon.app.ksr.service.EventService;
import com.seadragon.app.ksr.util.GsonUtils;

@Controller
public class EventsController {
	
	private static Logger logger = LoggerFactory.getLogger(LoginController.class);

	@Autowired
	private EventService eventService;
	
	@Autowired
	private EventScheduleService eventScheduleService;
	
	@RequestMapping(value = "/{entityType}/events.html", method = RequestMethod.GET)
	public String showEvents(@PathVariable String entityType) {
		return entityType+".events";
	}
//   
//	//get events list
//	@RequestMapping(value = "/business/events.json", method = RequestMethod.GET)
//	public @ResponseBody
//	String getEventsListJson(HttpServletRequest request, HttpSession session) {
//		String eventsJson = null;
//		if (session.getAttribute(KsrConstants.SESSION_SCOPED_BUSINESS) != null) {
//			Business business = (Business )session.getAttribute(KsrConstants.SESSION_SCOPED_BUSINESS);
//			List<Event> events = eventService.findAllEventsBy(business);
//			if (events!=null)
//				eventsJson =GsonUtils.ObjectToGson(events);
//		}
//		return eventsJson;
//	}
//	
//	
//	//create/update an event
//	@RequestMapping(value = "/business/event/save.json", method = RequestMethod.POST)
//	@ResponseBody
//	public JSONObject createEvent(@RequestBody String eventJson,HttpSession session) {
//		logger.debug(eventJson);
//		JSONObject json = new JSONObject();
//		Event event = (Event)GsonUtils.GsonToObject(eventJson, Event.class);
//		Business business = (Business )session.getAttribute(KsrConstants.SESSION_SCOPED_BUSINESS);
//		// save event
//		if (business!=null&&business.getId()==event.getBusiness().getId()){
//			if (event.getId()>0){
//				eventService.update(event);
//			} else {
//				eventService.save(event);
//			}
//			json.put("status", "ok");
//		} else {
//			json.put("status","error:unauthorized action");
//		}
//		
//		return json;
//	}
//	
//	//update an event
//	@RequestMapping(value = "/business/event/update.json", method = RequestMethod.POST)
//	@ResponseBody
//	public JSONObject updateEvent(@RequestBody String eventJson,HttpSession session) {
//		logger.debug(eventJson);
//		Event event = (Event)GsonUtils.GsonToObject(eventJson, Event.class);
//		// update event
//		eventService.update(event);
//		
//		JSONObject json = new JSONObject();
//		json.put("status", "ok");
//		return json;
//	}
//	
//	//get eventSchedule list
//	@RequestMapping(value = "/business/eventSchedules.json", method = RequestMethod.POST)
//	public @ResponseBody
//	String getEventScheduleListJson(HttpServletRequest request, HttpSession session,@RequestBody String eventJson) {
//		String json = null;
//		if (session.getAttribute(KsrConstants.SESSION_SCOPED_BUSINESS) != null) {
//			Business business = (Business )session.getAttribute(KsrConstants.SESSION_SCOPED_BUSINESS);
//			Event event = (Event)GsonUtils.GsonToObject(eventJson, Event.class);
//			if (event.getBusiness()!=null&&event.getBusiness().getId()==business.getId()){
//				List<EventSchedule> eventSchedules = eventScheduleService.findAllEventSchedulesBy(event);
//				if (eventSchedules!=null)
//					json =GsonUtils.ObjectToGson(eventSchedules);
//			}
//		}
//		return json;
//	}
//	
//	
//	//create an eventSchedule
//	@RequestMapping(value = "/business/eventSchedule/create.json", method = RequestMethod.POST)
//	@ResponseBody
//	public JSONObject createEventSchedule(@RequestBody String esjson,HttpSession session) {
//		logger.debug(esjson);
//		EventSchedule eventSchedule = (EventSchedule)GsonUtils.GsonToObject(esjson, EventSchedule.class);
//		// save event
//		eventScheduleService.save(eventSchedule);
//		
//		JSONObject json = new JSONObject();
//		json.put("status", "ok");
//		return json;
//	}
//	
//	//update an eventSchedule
//	@RequestMapping(value = "/business/eventSchedule/update.json", method = RequestMethod.POST)
//	@ResponseBody
//	public JSONObject udpateEventSchedule(@RequestBody String esjson,HttpSession session) {
//		logger.debug(esjson);
//		EventSchedule eventSchedule = (EventSchedule)GsonUtils.GsonToObject(esjson, EventSchedule.class);
//		// save event
//		eventScheduleService.update(eventSchedule);
//		
//		JSONObject json = new JSONObject();
//		json.put("status", "ok");
//		return json;
//	}

}

